// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.utils;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-20 qiang.zhang $
 * 
 */
public class TableBorderCollisionArbiter {

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshBorderData".
     * 
     * @param data
     * @param style
     * @param width
     * @param color
     * @param rowIndex
     * @param colIndex
     */
    public static void refreshBorderData(int[] data, int style, int width, int color, int rowIndex, int colIndex) {
        refreshBorderData(data, style, width, color, rowIndex, colIndex, 0);
    }

    /**
     * Refresh the border data as per the current border setting and existing border setting, using the CSS2.0 border
     * collision algorithm: if the style is none, always lose; then compare the width, greater win; if width is equal,
     * compare the style, less win; or keep the original. This is the part 1 of the algorithm.
     */
    public static void refreshBorderData(int[] data, int style, int width, int color, int rowIndex, int colIndex, int type) {
        assert (data.length > 5);

        if (style != 0) {
            if (data[0] == 0) {
                data[0] = style;
                data[1] = width;
                data[2] = color;
                data[3] = rowIndex;
                data[4] = colIndex;
                data[5] = type;
                return;
            }
            if (type > data[5]) {
                data[0] = style;
                data[1] = width;
                data[2] = color;
                data[3] = rowIndex;
                data[4] = colIndex;
                data[5] = type;
            } else if (type < data[5]) {
                return;
            }
            if (width > data[1] || data[0] == 0) {
                data[0] = style;
                data[1] = width;
                data[2] = color;
                data[3] = rowIndex;
                data[4] = colIndex;
                data[5] = type;
            } else if (width == data[1] && style < data[0]) {
                data[0] = style;
                data[2] = color;
                data[3] = rowIndex;
                data[4] = colIndex;
                data[5] = type;
            }
        } else if (data[0] == 0) {
            // this is the default line width. set to distinguish from those
            // never set.
            data[1] = 1;
        }
    }
}
